<?php

Loader::using("Models::Entities::Recaladas");
Loader::using("ext::libs::ChromePhp");

/**
 * Description of AdministrarRecaladas
 *
 * @author JeyDotC
 * 
 * @export Modules
 * @export-metadata RequiredRoles:Agencia
 */
class AdministrarRecaladas extends Module {

    /**
     *
     * @var Recaladas 
     */
    private $recalada;

    /**
     *
     * @var RecaladasPuertos
     */
    private $recaladaPuerto;

    protected function onModuleReady() {
        $this->recalada = new Recaladas();
        $this->recaladaPuerto = new RecaladasPuertos();
    }

    public function index() {
        $recaladas = $this->components->Recaladas->getAll();

        $this->assign("recaladas", $recaladas);

        $this->display("index");
    }

    public function view($Id = null) {
        $id = $Id ? $Id : $this->request->get("id");

        $recalada = $this->components->Recaladas->getById($id);
        $recaladaPuerto = $this->components->Recaladas->getRecaladaPuerto($id);
        $archivos = $this->components->Recaladas->getArchivos($id);


        if ($this->request->getSession()->has("fileResult")) {
            $fileOperation = $this->request->getSession()->remove("fileResult");
            
            if ($fileOperation["succeed"] == true) {
                $this->addMessage(new Message("El archivo ha sido subido correctamente", "Archivo subido", Message::SUCCESS));
            } else {
                $message = $fileOperation["error"];
                $this->addMessage(new Message($message, "El archivo no pudo ser subido", Message::ERROR));
            }
        }

        $this->assign("recalada", $recalada);
        $this->assign("recaladaPuerto", $recaladaPuerto);
        $this->assign("archivos", $archivos);
        $this->assign("editUrl", $this->route->action("edit", array("id" => $id,)));
        $this->assign("uploadUrl", $this->route->moduleAction("AdministrarArchivos", "upload"));

        $this->display("view");
    }

    public function create() {
        $this->setupEditor("save");

        $this->display("create");
    }

    function edit($Id = null) {
        $id = $Id ? $Id : $this->request->get("id");
        $this->recalada = $this->components->Recaladas->getById($id);
        $this->cargarRecaladaPuerto($id);

        $this->setupEditor("update", "view", array("id" => $id,));

        $this->assign("viajeCargue", $this->recalada->getViajes1());
        $this->assign("viajeDescargue", $this->recalada->getViajes());
        $this->assign("sentidos", array("NW" => "NW", "NE" => "NE", "SW" => "SW", "SE" => "SE",));

        $this->display("edit");
    }

    public function save() {
        if ($this->request->submitted()) {

            $this->request->bind($this->recalada, true);

            $this->recalada = $this->components->Recaladas->create($this->recalada);

            $this->view($this->recalada->getIdRecalada());
        } else {
            $this->create();
        }
    }

    public function update() {
        if ($this->request->submitted()) {
            $id = $this->request->post("idRecalada");
            $this->recalada = $this->components->Recaladas->getById($id);

            $this->request->bind($this->recalada, true);

            $this->components->Recaladas->update($this->recalada);

            $this->view($this->recalada->getIdRecalada());
        } else {
            $this->edit();
        }
    }

    private function setupEditor($action, $back = "index", $backParams = array()) {
        $buques = $this->components->Buques->getAll();

        $this->assign("tiposRecaladas", array(
            "Carque/Descargue",
            "Cargue",
            "Descargue",
            "Forzosa",
            "Reparaci&oacute;n",
            "Turismo",
            "Traslado",
            "Otro",
        ));
        $this->assign("estados", array(
            "Programada",
            "Cancelada",
            "Anulada",
            "En Puerto",
            "Es Historico",
        ));
        $this->assign("recaladaPuerto", $this->recaladaPuerto);
        $this->assign("recalada", $this->recalada);
        $this->assign("action", $this->route->action($action));
        $this->assign("back", $this->route->action($back, $backParams));
        $this->assign("buques", $buques);
    }

    private function cargarRecaladaPuerto($idRecalada) {
        $recaladaPuerto = $this->components->Recaladas->getRecaladaPuerto($idRecalada);

        if ($recaladaPuerto) {
            $this->recaladaPuerto = $recaladaPuerto;
        }
    }

}

?>
